package com.yuke.cloud.service.wmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.dto.WmsStockOutDto;
import com.yuke.cloud.service.wmc.entity.WmsStockOut;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.service.WmsPurchaseReturnOrderService;
import com.yuke.cloud.service.wmc.service.WmsStockOutService;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zys on 2018/12/06.
*/
@Api(value = "WEB - WmsStockOutController", tags = "处理WmsStockOut相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/wms/stock_out")
public class WmsStockOutController extends BaseController {
    @Resource
    private WmsStockOutService wmsStockOutService;
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsPurchaseReturnOrderService wmsPurchaseReturnOrderService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "wmsStockOut", value = "该数据记录的POJO") @RequestBody WmsStockOut wmsStockOut) {
        wmsStockOutService.save(wmsStockOut);
       /* WmsStorage wmsStorage = new WmsStorage();
        wmsStorage.setSpecId(wmsStockOut.getSpecId());
        wmsStorage.setWarehouseId(wmsStockOut.getWarehouseId());
        List<WmsStorage> select = wmsStorageService.select(wmsStorage);
        WmsStorage wmsStorage1 = select.get(0);
        Integer number = wmsStorage1.getNumber();
        wmsStorage1.setNumber(number-wmsStockOut.getOutNumber());
        wmsStorageService.update(wmsStorage1);
        return WrapMapper.ok();*/
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        wmsStockOutService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "wmsStockOut", value = "需要修改的内容") @RequestBody WmsStockOut wmsStockOut) {
        wmsStockOutService.update(wmsStockOut);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<WmsStockOut> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        WmsStockOut wmsStockOut = wmsStockOutService.findById(id);
        return WrapMapper.ok(wmsStockOut);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<WmsStockOut>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<WmsStockOut> list = wmsStockOutService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 新增出库记录，并且减库存。扫描枪出库也在这个接口
     * @param wmsStockOutList
     * @return
     */
    @PostMapping("/app/wmsStockOut")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addWmsStockOut(@ApiParam(name = "wmsStockOut", value = "该数据记录的POJO") @RequestBody List<WmsStockOut> wmsStockOutList) {
        if (wmsStockOutList == null || wmsStockOutList.size()< 1){
            return WrapMapper.error();
        }

        for (WmsStockOut wmsStockOut: wmsStockOutList){
            if (wmsStockOut.getOutNumber() < 1){
                continue;
            }
            wmsStockOutService.save(wmsStockOut);

            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setSpecId(wmsStockOut.getSpecId());
            wmsStorage.setWarehouseId(wmsStockOut.getWarehouseId());
            List<WmsStorage> select = wmsStorageService.select(wmsStorage);
            wmsStorage = select.get(0);
            Integer number = wmsStorage.getNumber()-wmsStorage.getLockNumber()-wmsStockOut.getOutNumber();
            if (number>0){
                //只削减库存
                wmsStorage.setNumber(wmsStorage.getNumber()-wmsStockOut.getOutNumber());
            }else{
                wmsStorage.setNumber(wmsStorage.getLockNumber());
            }
            WmsStorage wmsStorage2 = new WmsStorage();
            wmsStorage2.setStorageId(wmsStorage.getStorageId());
            wmsStorage2.setNumber(wmsStorage.getNumber());
            wmsStorageService.update(wmsStorage2);
        }

        return WrapMapper.ok();
    }

    @PostMapping("/countOutBySpec")
    @ApiOperation(httpMethod = "POST", value = "根据单品统计")
    public Wrapper<PageInfo<WmsStockOutDto>> countOutBySpec(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                                @RequestBody WmsStockOutDto wmsStockOutDto){

        PageInfo<WmsStockOutDto> list = wmsStockOutService.countBySpec(wmsStockOutDto,page,size);

        return WrapMapper.ok(list);
    }

}
